package com.ugoodtech.mdcc.client.service.impl;

import com.mysema.query.BooleanBuilder;
import com.ugoodtech.mdcc.client.dto.JsonResponse;
import com.ugoodtech.mdcc.client.service.WeekServiceReportService;
import com.ugoodtech.mdcc.core.Constants;
import com.ugoodtech.mdcc.core.domain.*;
import com.ugoodtech.mdcc.core.domain.VstarUserKpiReport.VstarUserKpiReport;
import com.ugoodtech.mdcc.core.domain.VstarUserKpiReport.VstarUserKpiReport_Complete;
import com.ugoodtech.mdcc.core.domain.VstarUserKpiReport.VstarUserKpiReport_NeedFollowUp;
import com.ugoodtech.mdcc.core.domain.VstarUserKpiReport.VstarUserKpiReport_Plan;
import com.ugoodtech.mdcc.core.helper.SearchVoId;
import com.ugoodtech.mdcc.core.repository.*;
import com.ugoodtech.mdcc.core.service.SearchPermissionsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class WeekServiceReportServiceImpl implements WeekServiceReportService {

    @Autowired
    private QuestionAnswerOptionRepository questionAnswerOptionRepository;

    @Autowired
    private UserProfileRepository userProfileRepository;

    @Autowired
    private StationRegularServiceReportRepository stationRegularServiceReportRepository;

    @Autowired
    private StationColorReportRepository stationColorReportRepository;

    @Autowired
    private StationReportRepository stationReportRepository;

    @Autowired
    private DepartmentRepository departmentRepository;

    @Autowired
    private SearchPermissionsService searchPermissionsService;


    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat(Constants.DATE_FORMAT);

    /**
     * 获取技术人员kpi报告--改
     *
     * @return
     */
    @Override
    public JsonResponse statWeekReport(User clientUser, String userName, String stationName, String provinces, String brandname, String category, Long serviceStartTime, Long serviceEndTime) {
        QStationReport qStationReport = QStationReport.stationReport;
        BooleanBuilder builder = new BooleanBuilder();
        builder.and(qStationReport.deleted.eq(false));
        builder.and(qStationReport.enabled.eq(true));
        builder.and(qStationReport.category.eq(Constants.SERVICE_REPORT));
        UserProfile profile = userProfileRepository.findByUserId(clientUser.getId());
        SearchVoId searchVoId;
        if (clientUser.getUserType().equals(UserType.dealers)) {
            searchVoId = searchPermissionsService.getDealersStationId(profile, provinces, brandname);
        } else {
            searchVoId = searchPermissionsService.searchColorReportByProvinceBrand(profile, provinces, brandname);
        }
        if (searchVoId.getResultStationId().isEmpty()) {
            return JsonResponse.successResponseWithData(new VstarUserKpiReport());
        }
        builder.and(qStationReport.station.id.in(searchVoId.getResultStationId()));
        if (serviceStartTime != null && serviceStartTime != 0 && serviceEndTime != null && serviceEndTime != 0) {
            builder.and(qStationReport.startServiceTime.after(new Date(serviceStartTime)));
            builder.and(qStationReport.endServiceTime.before(new Date(serviceEndTime)));
        }
        if (StringUtils.isNotEmpty(category)) {
            builder.and(qStationReport.station.category.eq(category));
        }
        if (StringUtils.isNotEmpty(userName)) {
            QUserProfile qUserProfile = QUserProfile.userProfile;
            BooleanBuilder booleanBuilder = new BooleanBuilder();
            booleanBuilder.and(qUserProfile.deleted.eq(false));
            booleanBuilder.and(qUserProfile.enabled.eq(true));
            booleanBuilder.and(qUserProfile.realName.like("%" + userName + "%"));
            List<UserProfile> userProfileList = (List<UserProfile>) userProfileRepository.findAll(booleanBuilder);
            List<Long> userIds = new ArrayList<>();
            for (UserProfile user : userProfileList) {
                userIds.add(user.getUser().getId());
            }
            builder.and(qStationReport.user.id.in(userIds));
        }
        if (StringUtils.isNotEmpty(stationName)) {
            builder.and(qStationReport.station.name.like("%" + stationName + "%"));
        }
        Sort sort = new Sort(Sort.Direction.DESC, "user.id", "updateTime");
        List<StationReport> stationReportList = (List<StationReport>) stationReportRepository.findAll(builder, sort);
        VstarUserKpiReport vstarUserKpiReport = new VstarUserKpiReport();
        List<VstarUserKpiReport_Complete> vstarUserKpiReport_complete = new ArrayList<>();
        List<VstarUserKpiReport_NeedFollowUp> vstarUserKpiReport_needFollowUp = new ArrayList<>();
        List<VstarUserKpiReport_Plan> vstarUserKpiReport_plan = new ArrayList<>();
        for (StationReport stationReport : stationReportList) {
            StationRegularServiceReport answer = stationRegularServiceReportRepository.selectAnswer(stationReport.getId(), 181L);
            if (answer != null) {
                //获取其他信息
                VstarUserKpiReport_Complete complete = new VstarUserKpiReport_Complete();
                UserProfile workUser = userProfileRepository.findByUserId(stationReport.getUser().getId());
                if (workUser != null && StringUtils.isNotEmpty(workUser.getRealName())) {
                    complete.setWorkUser(workUser.getRealName());
                } else {
                    complete.setWorkUser("N/A");
                }
                //设置客户类型
                QuestionAnswerOption questionAnswer = null;
                if (stationReport.getStation().getCustomerType() != null && stationReport.getStation().getCustomerType() != 0) {
                    if (stationReport.getStation().getCustomerStat() == 366L) {
                        StationColorReport stationColorReport = stationColorReportRepository.selectReport(stationReport.getId(), 185L);
                        if (stationColorReport != null && StringUtils.isNotEmpty(stationColorReport.getAnswerOther())) {
                            complete.setCustomerType(stationColorReport.getAnswerOther());
                        }
                    }
                    questionAnswer = questionAnswerOptionRepository.findOne(stationReport.getStation().getCustomerType());
                    if (questionAnswer != null && StringUtils.isNotEmpty(questionAnswer.getName())) {
                        complete.setCustomerType(questionAnswer.getName());
                    }
                }
                //设置客户状态
                if (stationReport.getStation().getCustomerStat() != null && stationReport.getStation().getCustomerStat() != 0) {
                    if (stationReport.getStation().getCustomerStat() == 362L) {
                        StationColorReport stationColorReport = stationColorReportRepository.selectReport(stationReport.getId(), 184L);
                        if (stationColorReport != null && StringUtils.isNotEmpty(stationColorReport.getAnswerOther())) {
                            complete.setCustomerStat(stationColorReport.getAnswerOther());
                        }
                    }
                    questionAnswer = questionAnswerOptionRepository.findOne(stationReport.getStation().getCustomerStat());
                    if (questionAnswer != null && StringUtils.isNotEmpty(questionAnswer.getName())) {
                        complete.setCustomerStat(questionAnswer.getName());
                    }
                }
                //设置油漆品牌
                if (stationReport.getStation().getPaintBrand() != null && StringUtils.isNotEmpty(stationReport.getStation().getPaintBrand())) {
                    String[] paintBrnadIds = stationReport.getStation().getPaintBrand().split(",");
                    StringBuffer stringBuffer = new StringBuffer("");
                    for (String id : paintBrnadIds) {
                        if (StringUtils.isNotEmpty(id)) {
                            if (id.equals("280")) {
                                StationColorReport stationColorReport = stationColorReportRepository.selectReport(stationReport.getId(), 13L);
                                if (stationColorReport != null && StringUtils.isNotEmpty(stationColorReport.getAnswerOther())) {
                                    stringBuffer.append(stationColorReport.getAnswerOther() + ",");
                                }
                            } else {
                                questionAnswer = questionAnswerOptionRepository.findOne(Long.valueOf(id));
                                if (questionAnswer != null && StringUtils.isNotEmpty(questionAnswer.getName())) {
                                    stringBuffer.append(questionAnswer.getName() + ",");
                                }
                            }
                        }
                    }
                    complete.setPaintBrand(stringBuffer.toString());
                }
                //设置品牌定位
                if (stationReport.getStation().getProductPosition() != null && StringUtils.isNotEmpty(stationReport.getStation().getProductPosition())) {
                    String[] productIds = stationReport.getStation().getProductPosition().split(",");
                    StringBuffer stringBuffer = new StringBuffer("");
                    for (String id : productIds) {
                        if (StringUtils.isNotEmpty(id)) {
                            questionAnswer = questionAnswerOptionRepository.findOne(Long.valueOf(id));
                            if (questionAnswer != null && StringUtils.isNotEmpty(questionAnswer.getName())) {
                                stringBuffer.append(questionAnswer.getName() + ",");
                            }
                        }
                    }
                    complete.setProduct_position(stringBuffer.toString());
                }
                complete.setBrand(stationReport.getStation().getBrand());
                complete.setProvince(stationReport.getStation().getProvince());
                complete.setTime(simpleDateFormat.format(stationReport.getUpdateTime()));
                StationRegularServiceReport regularServiceReport = stationRegularServiceReportRepository.selectQuestionOther(stationReport.getId(), 155L);
                try {
                    String[] targeIds = regularServiceReport.getAnswerId().split(",");
                    StringBuffer stringBuffer = new StringBuffer("");
                    for (String id : targeIds) {
                        if (StringUtils.isNotEmpty(id)) {
                            if (id.equals("191")) {
                                if (regularServiceReport != null && StringUtils.isNotEmpty(regularServiceReport.getAnswerOther())) {
                                    stringBuffer.append(regularServiceReport.getAnswerOther() + ",");
                                }
                            } else if (id.equals("189")) {
                                if (StringUtils.isNotEmpty(stationReport.getAnswerOther())) {
                                    stringBuffer.append(stationReport.getAnswerOther() + ",");
                                } else {
                                    stringBuffer.append("未填写内容,请联系此工作人员");
                                }
                            } else {
                                QuestionAnswerOption answerOption = questionAnswerOptionRepository.findOne(Long.valueOf(id));
                                if (answerOption != null && StringUtils.isNotEmpty(answerOption.getName())) {
                                    stringBuffer.append(answerOption.getName() + ",");
                                }
                            }

                        }
                    }
                    complete.setWorkType(stringBuffer.toString());
                } catch (Exception e) {
                    e.printStackTrace();
                    complete.setWorkType("N/A");
                }
                complete.setCustomerName(stationReport.getStation().getName());
                complete.setCategory(stationReport.getStation().getCategory());
                complete.setStationNum(stationReport.getStation().getStationNum());
                //获取油漆服务商
                QStationColorReport qStationColorReport = QStationColorReport.stationColorReport;
                builder = new BooleanBuilder();
                builder.and(qStationColorReport.deleted.eq(false));
                builder.and(qStationColorReport.enabled.eq(true));
                builder.and(qStationColorReport.questionId.eq(14L));
                builder.and(qStationColorReport.stationReport.station.id.eq(stationReport.getStation().getId()));
                StationColorReport colorReport = stationColorReportRepository.findOne(builder);
                try {
                    complete.setServiceCompany(colorReport.getAnswerOther());
                } catch (Exception e) {
                    e.printStackTrace();
                    complete.setServiceCompany("N/A");
                }

                //封装工作地点
                if (stationReport.getStation() != null) {
                    Station station = stationReport.getStation();
                    if (StringUtils.isNotEmpty(station.getAddress())) {
                        complete.setAddress(station.getAddress());
                    } else {
                        String province = "";
                        String city = "";
                        if (StringUtils.isNotEmpty(station.getProvince())) {
                            province = station.getProvince();
                        }
                        if (StringUtils.isNotEmpty(station.getCity())) {
                            city = station.getCity();
                        }
                        complete.setAddress(province + city);
                    }
                } else {
                    complete.setAddress("");
                }

                //获取服务自我评价
                try {
                    regularServiceReport = stationRegularServiceReportRepository.selectQuestion(stationReport.getId(), 157L);
                    complete.setServicePoint(regularServiceReport.getAnswerOther());
                } catch (Exception e) {
                    e.printStackTrace();
                    complete.setServicePoint("N/A");
                }
                vstarUserKpiReport_complete.add(complete);
            }
        }
        vstarUserKpiReport.setVstarUserKpiReport_complete(vstarUserKpiReport_complete);

        for (StationReport stationReport : stationReportList) {
            StationRegularServiceReport answer = stationRegularServiceReportRepository.selectAnswer(stationReport.getId(), 181L);
            if ((answer != null && !answer.getAnswerId().equals("181")) && !answer.getAnswerId().equals("354") || answer == null) {
                VstarUserKpiReport_NeedFollowUp needFollowUp = new VstarUserKpiReport_NeedFollowUp();

                UserProfile workUser = userProfileRepository.findByUserId(stationReport.getUser().getId());
                if (workUser != null && StringUtils.isNotEmpty(workUser.getRealName())) {
                    needFollowUp.setWorkUser(workUser.getRealName());
                } else {
                    needFollowUp.setWorkUser("N/A");
                }
                needFollowUp.setProvince(stationReport.getStation().getProvince());
                needFollowUp.setCategory(stationReport.getStation().getCategory());
                needFollowUp.setStationNum(stationReport.getStation().getStationNum());
                needFollowUp.setBrand(stationReport.getStation().getBrand());

                //设置客户类型
                QuestionAnswerOption questionAnswer = null;
                if (stationReport.getStation().getCustomerType() != null && stationReport.getStation().getCustomerType() != 0) {
                    if (stationReport.getStation().getCustomerStat() == 366L) {
                        StationColorReport stationColorReport = stationColorReportRepository.selectReport(stationReport.getId(), 185L);
                        if (stationColorReport != null && StringUtils.isNotEmpty(stationColorReport.getAnswerOther())) {
                            needFollowUp.setCustomerType(stationColorReport.getAnswerOther());
                        }
                    }
                    questionAnswer = questionAnswerOptionRepository.findOne(stationReport.getStation().getCustomerType());
                    if (questionAnswer != null && StringUtils.isNotEmpty(questionAnswer.getName())) {
                        needFollowUp.setCustomerType(questionAnswer.getName());
                    }
                }
                //设置客户状态
                if (stationReport.getStation().getCustomerStat() != null && stationReport.getStation().getCustomerStat() != 0) {
                    if (stationReport.getStation().getCustomerStat() == 362L) {
                        StationColorReport stationColorReport = stationColorReportRepository.selectReport(stationReport.getId(), 184L);
                        if (stationColorReport != null && StringUtils.isNotEmpty(stationColorReport.getAnswerOther())) {
                            needFollowUp.setCustomerStat(stationColorReport.getAnswerOther());
                        }
                    }
                    questionAnswer = questionAnswerOptionRepository.findOne(stationReport.getStation().getCustomerStat());
                    if (questionAnswer != null && StringUtils.isNotEmpty(questionAnswer.getName())) {
                        needFollowUp.setCustomerStat(questionAnswer.getName());
                    }
                }
                //设置油漆品牌
                if (stationReport.getStation().getPaintBrand() != null && StringUtils.isNotEmpty(stationReport.getStation().getPaintBrand())) {
                    String[] paintBrnadIds = stationReport.getStation().getPaintBrand().split(",");
                    StringBuffer stringBuffer = new StringBuffer("");
                    for (String id : paintBrnadIds) {
                        if (StringUtils.isNotEmpty(id)) {
                            if (id.equals("280")) {
                                StationColorReport stationColorReport = stationColorReportRepository.selectReport(stationReport.getId(), 13L);
                                if (stationColorReport != null && StringUtils.isNotEmpty(stationColorReport.getAnswerOther())) {
                                    stringBuffer.append(stationColorReport.getAnswerOther() + ",");
                                }
                            } else {
                                questionAnswer = questionAnswerOptionRepository.findOne(Long.valueOf(id));
                                if (questionAnswer != null && StringUtils.isNotEmpty(questionAnswer.getName())) {
                                    stringBuffer.append(questionAnswer.getName() + ",");
                                }
                            }
                        }
                    }
                    needFollowUp.setPaintBrand(stringBuffer.toString());
                }
                //设置品牌定位
                if (stationReport.getStation().getProductPosition() != null && StringUtils.isNotEmpty(stationReport.getStation().getProductPosition())) {
                    String[] productIds = stationReport.getStation().getProductPosition().split(",");
                    StringBuffer stringBuffer = new StringBuffer("");
                    for (String id : productIds) {
                        if (StringUtils.isNotEmpty(id)) {
                            questionAnswer = questionAnswerOptionRepository.findOne(Long.valueOf(id));
                            if (questionAnswer != null && StringUtils.isNotEmpty(questionAnswer.getName())) {
                                stringBuffer.append(questionAnswer.getName() + ",");
                            }
                        }
                    }
                    needFollowUp.setProduct_position(stringBuffer.toString());
                }

                StationRegularServiceReport regularServiceReport = stationRegularServiceReportRepository.selectQuestion(stationReport.getId(), 156L);
                if (regularServiceReport != null) {
                    QuestionAnswerOption answerOption = questionAnswerOptionRepository.findOne(Long.valueOf(regularServiceReport.getAnswerId()));
                    List<String> departmentList = new ArrayList<>();
                    departmentList.add(answerOption.getName());
                    if (answerOption.getId().equals(182)) {
                        StationReport deparmentReport = regularServiceReport.getStationReport();
                        String[] departmentIds = deparmentReport.getDeparmentIds().split(",");
                        for (String id : departmentIds) {
                            if (StringUtils.isEmpty(id)) {
                                continue;
                            }
                            Department department = departmentRepository.findOne(Long.valueOf(id));
                            departmentList.add(department.getTotalName());
                        }
                    }
                    needFollowUp.setAbnormalSituation(departmentList);
                }

                regularServiceReport = stationRegularServiceReportRepository.selectQuestionOther(stationReport.getId(), 155L);
                needFollowUp.setDateTime(simpleDateFormat.format(stationReport.getUpdateTime()));
                if (regularServiceReport == null) {
                    needFollowUp.setCuntomerName("N/A");
                    needFollowUp.setWorkType("N/A");
                } else {
                    String[] targerIds = regularServiceReport.getAnswerId().split(",");
                    StringBuffer stringBuffer = new StringBuffer("");
                    for (String id : targerIds) {
                        if (StringUtils.isNotEmpty(id)) {
                            if (id.equals("191")) {
                                if (regularServiceReport != null && StringUtils.isNotEmpty(regularServiceReport.getAnswerOther())) {
                                    stringBuffer.append(regularServiceReport.getAnswerOther() + ",");
                                }
                            } else if (id.equals("189")) {
                                if (StringUtils.isNotEmpty(stationReport.getAnswerOther())) {
                                    stringBuffer.append(stationReport.getAnswerOther() + ",");
                                } else {
                                    stringBuffer.append("未填写内容,请联系此工作人员");
                                }
                            } else {
                                QuestionAnswerOption questionAnswerOption = questionAnswerOptionRepository.findOne(Long.valueOf(id));
                                if (questionAnswerOption != null&& questionAnswerOption.getName()!=null){
                                    String targer = questionAnswerOption.getName();
                                    stringBuffer.append(targer + ",");
                                }
                            }

                        }
                    }
                    needFollowUp.setCuntomerName(stationReport.getStation().getName());
                    needFollowUp.setWorkType(stringBuffer.toString());
                }
                //获取油漆服务商
                QStationColorReport qStationColorReport = QStationColorReport.stationColorReport;
                builder = new BooleanBuilder();
                builder.and(qStationColorReport.deleted.eq(false));
                builder.and(qStationColorReport.enabled.eq(true));
                builder.and(qStationColorReport.questionId.eq(14L));
                builder.and(qStationColorReport.stationReport.station.id.eq(stationReport.getStation().getId()));
                StationColorReport colorReport = stationColorReportRepository.findOne(builder);
                try {
                    needFollowUp.setServiceCompany(colorReport.getAnswerOther());
                } catch (Exception e) {
                    e.printStackTrace();
                    needFollowUp.setServiceCompany("N/A");
                }

                //封装工作地点
                if (stationReport.getStation() != null) {
                    Station station = stationReport.getStation();
                    if (StringUtils.isNotEmpty(station.getAddress())) {
                        needFollowUp.setAddress(station.getAddress());
                    } else {
                        String province = "";
                        String city = "";
                        if (StringUtils.isNotEmpty(station.getProvince())) {
                            province = station.getProvince();
                        }
                        if (StringUtils.isNotEmpty(station.getCity())) {
                            city = station.getCity();
                        }
                        needFollowUp.setAddress(province + city);
                    }
                } else {
                    needFollowUp.setAddress("");
                }


                //获取服务自我评价
                try {
                    regularServiceReport = stationRegularServiceReportRepository.selectQuestionOther(stationReport.getId(), 157L);
                    needFollowUp.setServicePoint(regularServiceReport.getAnswerOther());
                } catch (Exception e) {
                    needFollowUp.setServicePoint("N/A");
                }
                vstarUserKpiReport_needFollowUp.add(needFollowUp);
            }
        }
        vstarUserKpiReport.setVstarUserKpiReport_needFollowUp(vstarUserKpiReport_needFollowUp);

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(Constants.DATE_FORMAT);
        Station station = null;
        for (StationReport stationReport : stationReportList) {
            StationRegularServiceReport answer = stationRegularServiceReportRepository.selectAnswer(stationReport.getId(), 354L);
            if (answer != null) {
                VstarUserKpiReport_Plan plan = new VstarUserKpiReport_Plan();
                plan.setProvice(stationReport.getStation().getProvince());
                plan.setDateTime(simpleDateFormat.format(stationReport.getUpdateTime()));
                plan.setCategory(stationReport.getStation().getCategory());
                plan.setStationNum(stationReport.getStation().getStationNum());
                plan.setBrand(stationReport.getStation().getBrand());
                //设置客户类型
                QuestionAnswerOption questionAnswer = null;
                if (stationReport.getStation().getCustomerType() != null && stationReport.getStation().getCustomerType() != 0) {
                    if (stationReport.getStation().getCustomerStat() == 366L) {
                        StationColorReport stationColorReport = stationColorReportRepository.selectReport(stationReport.getId(), 185L);
                        if (stationColorReport != null && StringUtils.isNotEmpty(stationColorReport.getAnswerOther())) {
                            plan.setCustomerType(stationColorReport.getAnswerOther());
                        }
                    }
                    questionAnswer = questionAnswerOptionRepository.findOne(stationReport.getStation().getCustomerType());
                    if (questionAnswer != null && StringUtils.isNotEmpty(questionAnswer.getName())) {
                        plan.setCustomerType(questionAnswer.getName());
                    }
                }
                //设置客户状态
                if (stationReport.getStation().getCustomerStat() != null && stationReport.getStation().getCustomerStat() != 0) {
                    if (stationReport.getStation().getCustomerStat() == 362L) {
                        StationColorReport stationColorReport = stationColorReportRepository.selectReport(stationReport.getId(), 184L);
                        if (stationColorReport != null && StringUtils.isNotEmpty(stationColorReport.getAnswerOther())) {
                            plan.setCustomerStat(stationColorReport.getAnswerOther());
                        }
                    }
                    questionAnswer = questionAnswerOptionRepository.findOne(stationReport.getStation().getCustomerStat());
                    if (questionAnswer != null && StringUtils.isNotEmpty(questionAnswer.getName())) {
                        plan.setCustomerStat(questionAnswer.getName());
                    }
                }
                //设置油漆品牌
                if (stationReport.getStation().getPaintBrand() != null && StringUtils.isNotEmpty(stationReport.getStation().getPaintBrand())) {
                    String[] paintBrnadIds = stationReport.getStation().getPaintBrand().split(",");
                    StringBuffer stringBuffer = new StringBuffer("");
                    for (String id : paintBrnadIds) {
                        if (StringUtils.isNotEmpty(id)) {
                            if (id.equals("280")) {
                                StationColorReport stationColorReport = stationColorReportRepository.selectReport(stationReport.getId(), 13L);
                                if (stationColorReport != null && StringUtils.isNotEmpty(stationColorReport.getAnswerOther())) {
                                    stringBuffer.append(stationColorReport.getAnswerOther() + ",");
                                }
                            } else {
                                questionAnswer = questionAnswerOptionRepository.findOne(Long.valueOf(id));
                                if (questionAnswer != null && StringUtils.isNotEmpty(questionAnswer.getName())) {
                                    stringBuffer.append(questionAnswer.getName() + ",");
                                }
                            }
                        }
                    }
                    plan.setPaintBrand(stringBuffer.toString());
                }
                //设置品牌定位
                if (stationReport.getStation().getProductPosition() != null && StringUtils.isNotEmpty(stationReport.getStation().getProductPosition())) {
                    String[] productIds = stationReport.getStation().getProductPosition().split(",");
                    StringBuffer stringBuffer = new StringBuffer("");
                    for (String id : productIds) {
                        if (StringUtils.isNotEmpty(id)) {
                            questionAnswer = questionAnswerOptionRepository.findOne(Long.valueOf(id));
                            if (questionAnswer != null && StringUtils.isNotEmpty(questionAnswer.getName())) {
                                stringBuffer.append(questionAnswer.getName() + ",");
                            }
                        }
                    }
                    plan.setProduct_position(stringBuffer.toString());
                }
                //封装工作人员
                if (stationReport.getUser() != null) {
                    UserProfile userProfile = userProfileRepository.findByUserId(stationReport.getUser().getId());
                    if (userProfile != null && StringUtils.isNotEmpty(userProfile.getRealName())) {
                        plan.setWorkUser(userProfile.getRealName());
                    } else {
                        plan.setWorkUser("N/A");
                    }
                } else {
                    plan.setWorkUser("N/A");
                }

                //封装工作地点
                if (stationReport.getStation() != null) {
                    station = stationReport.getStation();
                    if (StringUtils.isNotEmpty(station.getAddress())) {
                        plan.setAddress(station.getAddress());
                    } else {
                        String province = "";
                        String city = "";
                        if (StringUtils.isNotEmpty(station.getProvince())) {
                            province = station.getProvince();
                        }
                        if (StringUtils.isNotEmpty(station.getCity())) {
                            city = station.getCity();
                        }
                        plan.setAddress(province + city);
                    }
                } else {
                    plan.setAddress("");
                }

                //封装客户名称
                if (stationReport.getStation() != null) {
                    station = stationReport.getStation();
                    if (StringUtils.isNotEmpty(station.getName())) {
                        plan.setCustomerUser(station.getName());
                    } else {
                        plan.setCustomerUser("N/A");
                    }
                } else {
                    plan.setCustomerUser("N/A");
                }

                //获取油漆服务商
                QStationColorReport qStationColorReport = QStationColorReport.stationColorReport;
                builder = new BooleanBuilder();
                builder.and(qStationColorReport.deleted.eq(false));
                builder.and(qStationColorReport.enabled.eq(true));
                builder.and(qStationColorReport.questionId.eq(14L));
                builder.and(qStationColorReport.stationReport.station.id.eq(stationReport.getStation().getId()));
                StationColorReport colorReport = stationColorReportRepository.findOne(builder);
                try {
                    plan.setServiceCompany(colorReport.getAnswerOther());
                } catch (Exception e) {
                    e.printStackTrace();
                    plan.setServiceCompany("N/A");
                }

                //获取服务自我评价
                try {
                    StationRegularServiceReport regularServiceReport = stationRegularServiceReportRepository.selectQuestion(stationReport.getId(), 157L);
                    plan.setServicePoint(regularServiceReport.getAnswerOther());
                } catch (Exception e) {
                    e.printStackTrace();
                    plan.setServicePoint("N/A");
                }
                StationRegularServiceReport regularServiceReport = stationRegularServiceReportRepository.selectQuestionOther(stationReport.getId(), 155L);
                if (regularServiceReport != null && StringUtils.isNotEmpty(regularServiceReport.getAnswerId())) {
                    String[] targeIds = regularServiceReport.getAnswerId().split(",");
                    StringBuffer stringBuffer = new StringBuffer("");
                    for (String id : targeIds) {
                        if (id.equals("191")) {
                            if (regularServiceReport != null && StringUtils.isNotEmpty(regularServiceReport.getAnswerOther())) {
                                stringBuffer.append(regularServiceReport.getAnswerOther() + ",");
                            }
                        } else if (id.equals("189")) {
                            if (StringUtils.isNotEmpty(stationReport.getAnswerOther())) {
                                stringBuffer.append(stationReport.getAnswerOther() + ",");
                            } else {
                                stringBuffer.append("未填写内容,请联系此工作人员");
                            }
                        } else {
                            QuestionAnswerOption answerOption = questionAnswerOptionRepository.findOne(Long.valueOf(id));
                            if (answerOption != null && StringUtils.isNotEmpty(answerOption.getName())) {
                                stringBuffer.append(answerOption.getName() + ",");
                            }
                        }

                    }
                    plan.setWorkType(stringBuffer.toString());
                } else {
                    plan.setWorkType("");
                }

                vstarUserKpiReport_plan.add(plan);
            }
        }
        vstarUserKpiReport.setVstarUserKpiReport_Plan(vstarUserKpiReport_plan);
        return JsonResponse.successResponseWithData(vstarUserKpiReport);
    }

    private String getProvinceStr(List<Nation> nations) {
        StringBuffer stringBuffer = new StringBuffer("");
        for (Nation nation : nations) {
            stringBuffer.append(nation.getProvince() + ",");
        }
        return stringBuffer.toString();
    }


    public JsonResponse statPersonRTeport(Long userId, Long stationId) {
        QStationReport qStationReport = QStationReport.stationReport;
        BooleanBuilder builder = new BooleanBuilder();
        builder.and(qStationReport.deleted.eq(false));
        builder.and(qStationReport.enabled.eq(true));
        builder.and(qStationReport.category.eq(Constants.SERVICE_REPORT));
        builder.and(qStationReport.user.id.eq(userId));
        if (stationId != null && stationId != 0) {
            builder.and(qStationReport.station.id.eq(stationId));
        }
        List<StationReport> stationReportList = (List<StationReport>) stationReportRepository.findAll(builder);
        VstarUserKpiReport vstarUserKpiReport = new VstarUserKpiReport();
        List<VstarUserKpiReport_Complete> vstarUserKpiReport_complete = new ArrayList<>();
        List<VstarUserKpiReport_NeedFollowUp> vstarUserKpiReport_needFollowUp = new ArrayList<>();
        List<VstarUserKpiReport_Plan> vstarUserKpiReport_plan = new ArrayList<>();

        UserProfile userProfile = userProfileRepository.findByUserId(userId);
        if (userProfile != null && StringUtils.isNotEmpty(userProfile.getRealName())) {
            vstarUserKpiReport.setUserName(userProfile.getRealName());
        } else {
            vstarUserKpiReport.setUserName("暂无");
        }

        for (StationReport stationReport : stationReportList) {
            StationRegularServiceReport answer = stationRegularServiceReportRepository.selectAnswer(stationReport.getId(), 181L);

        }
        return null;
    }


}
